﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Newtonsoft.Json.Linq;
using XHD.WXZJ.Authorization.Roles;
using XHD.WXZJ.MultiTenancy;
using XHD.WXZJ.Users;

namespace XHD.WXZJ.Web.Controllers.RolesManager
{
    public class RolesMgrController : WXZJControllerBase
    {
        // GET: RoleManager
        public ActionResult Index()
        {
            return View();
        }

        private readonly TenantManager _tenantManager;
        private readonly UserManager _userManager;
        private readonly RoleManager _roleManager;
        private readonly IUnitOfWorkManager _unitOfWorkManager;
        private readonly IRepository<Role> _roleRepository;
        private readonly RoleStore _roleStore;
        public RolesMgrController(
            TenantManager tenantManager,
            UserManager userManager,
            RoleManager roleManager,
            IUnitOfWorkManager unitOfWorkManager,
            IRepository<Role> roleRepository,
            RoleStore roleStore
            )
        {
            _tenantManager = tenantManager;
            _userManager = userManager;
            _roleManager = roleManager;
            _unitOfWorkManager = unitOfWorkManager;
            _roleRepository = roleRepository;
            _roleStore = roleStore;
        }

        [HttpPost]
        [UnitOfWork]
        public JObject GetAllRoles()
        {
            JObject ret = new JObject();
            var roles = _roleRepository.GetAllList();
            var rows = new JArray();
            foreach (var role in roles)
            {
                var r = new JObject();
                r.Add(new JProperty("id", role.Id));
                r.Add(new JProperty("name", role.Name));
                r.Add(new JProperty("displayName", role.DisplayName));
                r.Add(new JProperty("creationTime", role.CreationTime.ToString("YYYY-MM-DD hh:mm:ss")));
                rows.Add(r);
            }
            ret.Add(new JProperty("rows", rows));
            ret.Add(new JProperty("total", roles.Count()));
            return ret;
        }

       
        public JObject AddRole(JObject input)
        {
            JObject ret = new JObject();
            var role = new Role(_roleManager.AbpSession.TenantId,"TESTRole","测试角色");
            var result = _roleRepository.InsertOrUpdateAndGetId(role);
            ret.Add(new JProperty("result", result));
            return ret;
        }
      
    }
}
